﻿using Cache;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Text;

namespace Cache
{
    /// <summary>
    /// 全局数据
    /// </summary>
    public class GlobalData
    {
        #region 属性-配置

        /// <summary>
        /// 配置文件
        /// </summary>
        public static IConfiguration Configuration { get; set; }

        /// <summary>
        /// 应用令牌
        /// </summary>
        public static AppConfig AppConfig { get; set; } = new AppConfig();

        /// <summary>
        /// JWT配置
        /// </summary>
        public static JWTConfig JwtConfig { get; set; } = new JWTConfig();


        /// <summary>
        /// 数据库连接
        /// </summary>
        public static DbConnect DbConnect { get; set; } = new DbConnect();

        /// <summary>
        /// 缓存配置
        /// </summary>
        public static CacheConfig CacheConfig { get; set; } = new CacheConfig();

        /// <summary>
        /// 获取注入
        /// </summary>
        public static IServiceProvider ServiceLocator { get; set; }

        /// <summary>
        /// 环境配置
        /// </summary>
        public static EnvConfig EnvConfig { get; set; } = new EnvConfig();

        /// <summary>
        /// 授权配置
        /// </summary>
        public static CorsConfig CorsConfig { get; set; } = new CorsConfig();
        /// <summary>
        /// 物品配置
        /// </summary>
        public static EvidenceInfo EvidenceInfo { get; set; } = new EvidenceInfo();

        public static string WebPath = "";

        #endregion

        #region 属性-框架

        /// <summary>
        /// 缓存
        /// </summary>
        public static ICache Cache { get; set; }

        /// <summary>
        /// 日志
        /// </summary>
        //public static ILogger Logger { get; set; }

        #endregion

        #region 初始化数据：Init

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="act">初始化后的方法</param>
        /// <remarks>首先要给配置类赋值</remarks>
        public static void Init(Action act = null)
        {
            var isWindows = Environment.OSVersion.VersionString.ToUpper().IndexOf("WINDOWS") >= 0;
            Configuration.GetSection("App").Bind(AppConfig);
            Configuration.GetSection("JWT").Bind(JwtConfig);
            Configuration.GetSection("DbConnect").Bind(DbConnect);
            Configuration.GetSection("CacheConfig").Bind(CacheConfig);
            Configuration.GetSection("Env").Bind(EnvConfig);
            Configuration.GetSection("EvidenceInfo").Bind(EvidenceInfo);
            act?.Invoke();
        }

        #endregion
    }



    /// <summary>
    /// Jwt配置
    /// </summary>
    public class JWTConfig
    {
        /// <summary>
        /// 发版者
        /// </summary>
        public string Issuer { get; set; }

        /// <summary>
        /// 接收者
        /// </summary>
        public string Audience { get; set; }

        /// <summary>
        /// 密钥
        /// </summary>
        public string SignKey { get; set; }

        /// <summary>
        /// 过期时间
        /// </summary>
        public int ExpiresMinutes { get; set; }


        /// <summary>
        /// 刷新接收人
        /// </summary>
        public string RefreshTokenAudience { get; set; }

        public int RefreshTokenExpiresMinutes { get; set; }
    }
    /// <summary>
    /// 数据库连接
    /// </summary>
    public class DbConnect
    {
        /// <summary>
        /// MYSQL数据库
        /// </summary>
        public string Mysql { get; set; }

        /// <summary>
        /// 联创版mysql数据库
        /// </summary>
        public string SecondMysql { get; set; }

        /// <summary>
        /// MongoDB数据
        /// </summary>
        public string MongoDB { get; set; }
        /// <summary>
        /// SqlServer
        /// </summary>
        public string SqlServer { get; set; }

        public string SqlServerMKHIS { get; set; }

        public string SqlServerMKBASE { get; set; }
    }

    #region 应用配置类：AppConfig

    /// <summary>
    /// 应用
    /// </summary>
    public class AppConfig
    {
        /// <summary>
        /// 应用键
        /// </summary>
        public string Key { get; set; }

        /// <summary>
        /// 应用密钥
        /// </summary>
        public string Secret { get; set; }

        /// <summary>
        /// 域名
        /// </summary>
        public string Domain { get; set; }

    }

    #endregion

    #region 环境配置类：EnvConfig

    /// <summary>
    /// 环境模式
    /// </summary>
    public enum EnvModule
    {
        /// <summary>
        /// 开发模式
        /// </summary>
        Development,

        /// <summary>
        /// 演示模式
        /// </summary>
        Staging,

        /// <summary>
        /// 生产模式
        /// </summary>
        Production,
    }

    /// <summary>
    /// 环境
    /// </summary>
    public class EnvConfig
    {
        /// <summary>
        /// 模式。环境变量设置：ASPNETCORE_ENVIRONMENT：
        /// </summary>
        public EnvModule Module { get; set; }

        /// <summary>
        /// 路径
        /// </summary>
        public string Path { get; set; }

        /// <summary>
        /// 域名
        /// </summary>
        public string Domain { get; set; }

        /// <summary>
        /// 文件版本号
        /// </summary>
        public string FileVersion { get; set; }

        /// <summary>
        /// 是否读取站点数据
        /// </summary>
        public bool SiteConfigabled { get; set; } = true;

        /// <summary>
        /// 是否为异步模式
        /// </summary>
        public bool IsAsyncMode { get; set; } = false;

        /// <summary>
        /// 通用错误信息
        /// </summary>
        public string ErrInfo { get; set; }

        /// <summary>
        /// 脱敏的密钥
        /// </summary>
        public string AESKey { get; set; }
    }

    #endregion

    /// <summary>
    /// 授权配置
    /// </summary>
    public class CorsConfig
    {
        /// <summary>
        /// 前端页面跳转
        /// </summary>
        public string MvcClientRedirect { get; set; }

        /// <summary>
        /// 前端页面退出
        /// </summary>
        public string MvcClientLogout { get; set; }

        /// <summary>
        /// 前端页面跨域
        /// </summary>
        public string[] MvcClientOrigins { get; set; }

        /// <summary>
        /// js客户端跳转
        /// </summary>
        public string[] JsClientRedirect { get; set; }

        /// <summary>
        /// js客户端退出
        /// </summary>
        public string[] JsClientLogout { get; set; }

        /// <summary>
        /// js客户端跨域
        /// </summary>
        public string[] JsClientOrigins { get; set; }
    }

    public static class ServiceLocator
    {
        public static IServiceProvider Instance { get; set; }
    }

    public  class EvidenceInfo 
    {
        public  string BaseImgUrl { get; set; }
        public string RfidWran { get; set; }
        public string Readepc { get; set; }
        public string Taskdown { get; set; }
        public string Propertyprint { get; set; }
    }
}
